package com.eonmind.user.controller;

import com.eonmind.common.contract.data.response.PageResponse;
import com.eonmind.common.contract.data.response.ResponseEntity;
import com.eonmind.common.security.annotations.UserInfo;
import com.eonmind.user.contract.dto.UserInfoData;
import com.eonmind.user.dto.UserDto;
import com.eonmind.user.service.UserService;
import com.eonmind.user.utils.DesensitizationUtil;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户模块
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 注册
     *
     * @param userRegisterRequest 成功返回true，失败返回false
     */
    @PostMapping("/register")
    public ResponseEntity<Boolean> register(@RequestBody UserDto.UserRegisterRequest userRegisterRequest) {
        boolean result = userService.register(userRegisterRequest);
        if (result) {
            return ResponseEntity.success();
        } else {
            return ResponseEntity.fail("注册失败，用户名可能已存在");
        }
    }

    /**
     * 用户登录
     *
     * @param userLoginRequest 用户登录参数
     * @return userLoginResponse
     * @apiNote 用户传入用户名或者邮箱或者手机号，和密码 进行登录
     */
    @PostMapping("/login")
    public ResponseEntity<UserDto.UserLoginResponse> login(@RequestBody @Validated UserDto.UserLoginRequest userLoginRequest, HttpServletRequest request) {
        UserDto.UserLoginResponse userLoginResponse = userService.login(userLoginRequest, request);
        return ResponseEntity.success(DesensitizationUtil.desensitization(userLoginResponse));
    }


    /**
     * 用户修改密码
     *
     * @param userUpdateRequest 用户修改密码参数
     *                          成功返回true,失败返回false
     * @apiNote 用户传入电话号码/邮箱号和新密码 进行修改
     */
    @UserInfo
    @PostMapping("/update")
    public ResponseEntity<Boolean> update(UserInfoData userInfoData, @RequestBody UserDto.UserUpdateRequest userUpdateRequest) {
        if (userInfoData != null && StringUtils.compare(userInfoData.getEmail(), userUpdateRequest.getEmail()) != 0) {
            log.error("邮箱号与当前登录用户邮箱不一致：{}，{}", userInfoData, userUpdateRequest);
            return ResponseEntity.fail("邮箱号与当前登录用户邮箱不一致");
        }
        boolean result = userService.update(userUpdateRequest);
        if (result) {
            return ResponseEntity.success();
        } else {
            return ResponseEntity.fail("修改密码失败");
        }
    }

    /**
     * 发送注册验证码
     *
     * @apiNote 用户传入邮箱号进行发送验证码
     * 验证码发送成功返回true,失败返回false
     */
    @PostMapping("/sendRegisterVerificationCode")
    public ResponseEntity<Boolean> sendRegisterVerificationCode(@RequestBody UserDto.SendEmailCodeRequest sendEmailCodeRequest) {

        userService.sendRegisterVerificationCode(sendEmailCodeRequest.getEmail());
        return ResponseEntity.success();
    }

    /**
     * 发送修改密码验证码
     *
     * @ignoreParams userInfoData
     * @apiNote 用户传入邮箱号进行发送验证码
     * 验证码发送成功返回true,失败返回false
     * 如果登录状态调用，就是修改密码逻辑
     */
    @UserInfo
    @PostMapping("/sendUpdateVerificationCode")
    public ResponseEntity<Boolean> sendUpdateVerificationCode(UserInfoData userInfoData, @RequestBody UserDto.SendEmailCodeRequest sendEmailCodeRequest) {
        if (userInfoData != null && StringUtils.compare(userInfoData.getEmail(), sendEmailCodeRequest.getEmail()) != 0) {
            log.error("邮箱号与当前登录用户邮箱不一致：{}，{}", userInfoData, sendEmailCodeRequest);
            return ResponseEntity.fail("邮箱号与当前登录用户邮箱不一致");
        }
        userService.sendUpdateVerificationCode(sendEmailCodeRequest.getEmail());
        return ResponseEntity.success();
    }

    /**
     * 用户登录记录分页查询
     *
     * @param userLoginRecordPageRequest
     * @return
     * @ignoreParams userInfoData
     */
    @UserInfo
    @PostMapping("/pageUserLoginRecord")
    public ResponseEntity<PageResponse<UserDto.UserLoginRecordData>>
    pageUserLoginRecord(UserInfoData userInfoData, @RequestBody UserDto.UserLoginRecordPageRequest userLoginRecordPageRequest) {
        return ResponseEntity.success(userService.pageUserLoginRecord(userInfoData, userLoginRecordPageRequest));
    }
}
